<?php

namespace App\Console\Commands;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Common\Models\Db1\AliApp;
use App\Common\Util;
use Illuminate\Console\Command;
//use TopClient;

class Test1 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test1';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        include app_path() . "/Common/Lib/taobao-sdk-PHP-auto/TopSdk.php";
        date_default_timezone_set('Asia/Shanghai');
        $c = new \TopClient;
        $c->appkey = '27767643';
        $c->secretKey = '3bcb2b445dead97ccede1501656f7f39';

        //接口权限申请在https://open.alimama.com/

        $req = new \TbkCommonRequest();
        $req->setInfo('taobao.product.get' , [
            'fields' => "product_id,outer_id",
            'product_id' => '86126527',
            'cid' => '50012286',
            'props' => '10005:10027;10006:29729'
        ]);
        $req->setInfo('taobao.tbk.item.info.get' , [
            'num_iids' => '600089384372'
        ]);
        $resp = $c->execute($req);
        var_dump(Util::xml2arr($resp));
        exit();

        $req = new \TbkJuTqgGetRequest;
        $req->setAdzoneId("109465550021");
        $req->setFields("click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time");
        $req->setStartTime("2020-01-11 09:00:00");
        $req->setEndTime("2020-01-14 16:00:00");
        $req->setPageNo("1");
        $req->setPageSize("40");
        $resp = $c->execute($req);
        var_dump(Util::xml2arr($resp));
        exit();

        $req = new \TbkTpwdCreateRequest;
        $req->setUserId("123");
        $req->setText("长度大于5个字符");
        $req->setUrl("https://uland.taobao.com/");
        $req->setLogo("https://uland.taobao.com/");
        $req->setExt("{}");
        $resp = $c->execute($req);
        var_dump(Util::xml2arr($resp));
        exit();

        /*
        $req = new \TimeGetRequest;
        $resp = $c->execute($req);
        var_dump(Util::xml2arr($resp));
        exit();
        */

        $req = new \HttpdnsGetRequest;

        $req->putOtherTextParam("name","test");
        $req->putOtherTextParam("value",0);

        $resp = $c->execute($req);
        var_dump(Util::xml2arr($resp));
        exit();

        $item_ali_app = (new AliApp())->findOne(['id' => 2]);
        AlibabaCloud::accessKeyClient($item_ali_app->access_key_id, $item_ali_app->access_key)
            ->regionId('cn-beijing')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('Ecs')
                // ->scheme('https') // https | http
                ->version('2014-05-26')
                ->action('DescribePrice')
                ->method('POST')
                ->host('ecs.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-beijing",
                        'InstanceId' => "i-2zeg2ccp2xohwwnof3no"
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }

        try {
            $result = AlibabaCloud::rpc()
                ->product('Ecs')
                // ->scheme('https') // https | http
                ->version('2014-05-26')
                ->action('DescribeInstances')
                ->method('POST')
                ->host('ecs.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-beijing",
                        'Name' => "pwd",
                        'Type' => "RunShellScript",
                        'CommandContent' => "ls",
                        'InstanceId.1' => "i-2zeg2ccp2xohwwnof3no",
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }

        exit();
    }
}
